package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentCompanyWebProfileBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberCompanyHeaderBean;
import com.sgb.member.entity.MemberProfileBean;
import com.sgb.member.ui.activity.MemberCompanyWebActivity;
import com.sgb.member.view_model.CompanyWebProfileModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.StringUtil;

import javax.inject.Inject;

/**
 * 会员企业网站-企业简介
 */
public class CompanyWebProfileFragment extends AppLazyFragment {

    @Inject
    CompanyWebProfileModel sModel;
    private FragmentCompanyWebProfileBinding mBinding;
    private MemberBean memberBean;

    public static CompanyWebProfileFragment newInstance(int position, MemberBean bean) {
        CompanyWebProfileFragment fragment = new CompanyWebProfileFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putSerializable("COMPANY_ID", bean);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_company_web_profile, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        assert getArguments() != null;
        memberBean = (MemberBean) getArguments().getSerializable("COMPANY_ID");

        mBinding.locationMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtils.isDoubleClick()) {
                    if (getActivity() != null) {
                        MemberCompanyHeaderBean headerBean = ((MemberCompanyWebActivity) getActivity()).getHeaderBean();
                        if (headerBean != null && !StringUtil.isEmpty(headerBean.latitude) && !StringUtil.isEmpty(headerBean.longitude)) {
                            MemberConfig.getInstance().getInstance().companyLocationMap(getContext(), headerBean.longitude,
                                    headerBean.latitude,
                                    headerBean.businessAddress,
                                    headerBean.companyName);
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        sModel.getCompanyProfile(memberBean);
    }

    public void setMemberProfile(MemberProfileBean data) {
        mBinding.layoutEmpty.setVisibility(View.GONE);
        mBinding.setItemBean(data);

        if (StringUtil.isEmpty(data.getIntroduction())) {
            mBinding.tvCompanyIntro.setText("");
        } else {
            String introduction = data.getIntroduction();
            String s = introduction.replaceAll("<p>", "").replaceAll("</p>", "");
            mBinding.tvCompanyIntro.setText(s);
        }
        String s = StringUtil.getNotEmptyStr(data.getIndustryCategoryOneName())
                + "/" + StringUtil.getNotEmptyStr(data.getIndustryCategoryTwoName());
        mBinding.tvIndustryCategory.setText(StringUtil.getWebProfile("行业分类：", s));
    }

    public void updateMemberInfo(String memberIntro, String remark) {
        if (mBinding != null) {
            mBinding.tvMemberIntro.setText(StringUtil.isEmpty(memberIntro) ? "/" : memberIntro);
            mBinding.tvRemark.setText(StringUtil.isEmpty(remark) ? "/" : remark);
        }
    }

    public void showEmptyView() {
        mBinding.layoutEmpty.setVisibility(View.VISIBLE);
    }

}
